package com.chenxingnet.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.chenxingnet.dto.Accident;
import com.chenxingnet.dto.Drivers;
import com.chenxingnet.service.DriversService;

@Controller
@RequestMapping("/drivers")
public class DriversController {
	@Resource
	private DriversService driversService;
	
	/**
	 * 转至新增驾驶员信息页面
	 * @return
	 */
	@RequestMapping("/insertDrivers")
	public String insertDrivers(HttpServletRequest request,Model model){
		try {
			Integer deptid = Integer.valueOf(request.getParameter("deptid"));
			if(deptid != null){
				model.addAttribute("deptid", deptid);
			}
		} catch (Exception e) {
			
		}
		return "driver_edit";
	}
	/**
	 * 转至修改页面
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("/showDriversById")
	public String showDriversById(HttpServletRequest request,Model model){
		try {
			Integer deptid = Integer.valueOf(request.getParameter("deptid"));
			if(deptid != null){
				model.addAttribute("deptid", deptid);
			}
		} catch (Exception e) {
			
		}
		Integer did = Integer.valueOf(request.getParameter("did"));
		Drivers driver = driversService.selectById(did);
		model.addAttribute("driver", driver);
		
		return "driver_edit";
	}
	
	
	/**
	 * 新增驾驶员信息
	 * @param request
	 * @param model
	 * @return,headers = "content-type=multipart/*", method = RequestMethod.POST
	 */
	@RequestMapping("/addDrivers")
	public String addDrivers(Drivers drivers,HttpServletRequest request,Model model,@RequestParam  MultipartFile... files){
		try {
			Integer deptid = Integer.valueOf(request.getParameter("deptid"));
			if(deptid != null){
				model.addAttribute("deptid", deptid);
			}
		} catch (Exception e) {
			
		}
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH");//小写的mm表示的是分钟HH:mm:ss
		
		if(drivers.getDid()==null){
			//调用新增方法
			String daccidentStr = request.getParameter("daccident1");
			String daudittimeStr = request.getParameter("daudittime1");
			String dauditdateStr = request.getParameter("dauditdate1");
			//意外保险时间
			Date daccident = null;
			//身份证审核时间
			Date daudittime = null;
			//登记证审核时间
			Date dauditdate = null;
			
			try {
				if(!daccidentStr.equals("")){
					daccident = sdf.parse(daccidentStr);
					drivers.setDaccident(daccident);
				}
				if(!daudittimeStr.equals("")){
					daudittime = sdf.parse(daudittimeStr);
					drivers.setDaudittime(daudittime);
				}
				if(!dauditdateStr.equals("")){
					dauditdate = sdf.parse(dauditdateStr);
					drivers.setDauditdate(dauditdate);
				}
			} catch (ParseException e) {
				e.printStackTrace();
			}
			//身份证图片
			String dpicture=files[0].getOriginalFilename();
			if(dpicture.contains("png")||dpicture.contains("PNG")||dpicture.contains("jpg")||dpicture.contains("JPG")
					||dpicture.contains("gif")||dpicture.contains("GIF")){//判断传过来的文件的类型
	    		String  imgfileDir=	driversService.saveImg(files[0],"png", 0);
	    		if(imgfileDir!=null&&!imgfileDir.equals("")){
	    			drivers.setDpicture(imgfileDir);
	    		}
			}
			
			//登记证图片
			String dregistrationcertificate =files[1].getOriginalFilename();
			if(dregistrationcertificate.contains("png")||dregistrationcertificate.contains("PNG")||dpicture.contains("jpg")||dpicture.contains("JPG")
					||dpicture.contains("gif")||dpicture.contains("GIF")){//判断传过来的文件的类型
	    		String  imgfileD = driversService.saveImg(files[1],"png", 1);
	    		if(imgfileD!=null&&!imgfileD.equals("")){
	    			drivers.setDregistrationcertificate(imgfileD);
	    		}
			}
			drivers.setDpubdate(new Date());
			drivers.setDreportstart("1");
			//调用service层新增驾驶员方法
			driversService.addDrivers(drivers);
		}else{
			String daccidentStr = request.getParameter("daccident1");
			String daudittimeStr = request.getParameter("daudittime1");
			String dauditdateStr = request.getParameter("dauditdate1");
			//意外保险时间
			Date daccident = null;
			//身份证审核时间
			Date daudittime = null;
			//登记证审核时间
			Date dauditdate = null;
			
			try {
				if(!daccidentStr.equals("")){
					daccident = sdf.parse(daccidentStr);
					drivers.setDaccident(daccident);
				}
				if(!daudittimeStr.equals("")){
					daudittime = sdf.parse(daudittimeStr);
					drivers.setDaudittime(daudittime);
				}
				if(!dauditdateStr.equals("")){
					dauditdate = sdf.parse(dauditdateStr);
					drivers.setDauditdate(dauditdate);
				}
			} catch (ParseException e) {
				e.printStackTrace();
			}
			//身份证图片
			String dpicture=files[0].getOriginalFilename();
			if(dpicture.contains("png")||dpicture.contains("PNG")||dpicture.contains("jpg")||dpicture.contains("JPG")
					||dpicture.contains("gif")||dpicture.contains("GIF")){//判断传过来的文件的类型
	    		String  imgfileDir=	driversService.saveImg(files[0],"png", 0);
	    		if(imgfileDir!=null&&!imgfileDir.equals("")){
	    			drivers.setDpicture(imgfileDir);
	    		}
			}
			
			//登记证图片
			String dregistrationcertificate =files[1].getOriginalFilename();
			if(dregistrationcertificate.contains("png")||dregistrationcertificate.contains("PNG")||dpicture.contains("jpg")||dpicture.contains("JPG")
					||dpicture.contains("gif")||dpicture.contains("GIF")){//判断传过来的文件的类型
	    		String  imgfileD = driversService.saveImg(files[1],"png", 1);
	    		if(imgfileD!=null&&!imgfileD.equals("")){
	    			drivers.setDregistrationcertificate(imgfileD);
	    		}
			}
			
			//调用service层修改驾驶员方法
			int i = driversService.updateDrivers(drivers);
			System.out.println("修改"+i+"条驾驶员信息");
		}
		
		
		return "redirect:/drivers/add_driver?pageNow=1";
	}
	
	
	/**
	* @Title: /drivers/mobile_addDrivers 
	*移动端新增驾驶员信息
	 */
	@RequestMapping("/mobile_addDrivers")
	public void mobile_addDrivers(Drivers drivers,HttpServletRequest request,Model model,HttpServletResponse response,@RequestParam  MultipartFile... files){
		
		try {
			
			response.setHeader("Access-Control-Allow-Origin", "*");
			response.setContentType("application/json;charset=utf-8");
			JSONObject returnJson = new JSONObject();
			returnJson.put("falg","true");
			
			try {
				Integer deptid = Integer.valueOf(request.getParameter("deptid"));
				if(deptid != null){
					returnJson.put("deptid", deptid);
				}
			} catch (Exception e) {
				
			}
			SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH");//小写的mm表示的是分钟HH:mm:ss
			
			String daccidentStr = request.getParameter("daccident1");
			String daudittimeStr = request.getParameter("daudittime1");
			String dauditdateStr = request.getParameter("dauditdate1");
			//意外保险时间
			Date daccident = null;
			//身份证审核时间
			Date daudittime = null;
			//登记证审核时间
			Date dauditdate = null;
			
			try {
				if(!daccidentStr.equals("")){
					daccident = sdf.parse(daccidentStr);
					drivers.setDaccident(daccident);
				}
				if(!daudittimeStr.equals("")){
					daudittime = sdf.parse(daudittimeStr);
					drivers.setDaudittime(daudittime);
				}
				if(!dauditdateStr.equals("")){
					dauditdate = sdf.parse(dauditdateStr);
					drivers.setDauditdate(dauditdate);
				}
			} catch (ParseException e) {
				e.printStackTrace();
			}
			//身份证图片
			String dpicture=files[0].getOriginalFilename();
			if(dpicture.contains("png")||dpicture.contains("PNG")||dpicture.contains("jpg")||dpicture.contains("JPG")
					||dpicture.contains("gif")||dpicture.contains("GIF")){//判断传过来的文件的类型
	    		String  imgfileDir=	driversService.saveImg(files[0],"png", 0);
	    		if(imgfileDir!=null&&!imgfileDir.equals("")){
	    			drivers.setDpicture(imgfileDir);
	    		}
			}
			
			//登记证图片
			String dregistrationcertificate =files[1].getOriginalFilename();
			if(dregistrationcertificate.contains("png")||dregistrationcertificate.contains("PNG")||dpicture.contains("jpg")||dpicture.contains("JPG")
					||dpicture.contains("gif")||dpicture.contains("GIF")){//判断传过来的文件的类型
	    		String  imgfileD = driversService.saveImg(files[1],"png", 1);
	    		if(imgfileD!=null&&!imgfileD.equals("")){
	    			drivers.setDregistrationcertificate(imgfileD);
	    		}
			}
			drivers.setDpubdate(new Date());
			drivers.setDreportstart("1");
			//调用service层新增驾驶员方法
			int resu = driversService.addDrivers(drivers);
			
			returnJson.put("result",resu);//返回插入结果。
			response.getWriter().print(returnJson.toString());
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		
	}
	
	
	/**
	 * 分页查询所有驾驶员信息
	 * @param model
	 * @return
	 */
	@RequestMapping("/add_driver")
	public String showDrivers(HttpServletRequest request,Model model){
		try {
			Integer deptid = Integer.valueOf(request.getParameter("deptid"));
			if(deptid != null){
				model.addAttribute("deptid", deptid);
			}
		} catch (Exception e) {
			
		}
		driversService.selectAllByCount(request, model);
		
		return "add_driver";
	}
	
	/**
	* @Title: /drivers/mobile_showDrivers 
	*移动端分页查询驾驶员信息
	 */
	@RequestMapping("/mobile_showDrivers")
	public void mobile_showDrivers(HttpServletRequest request,Model model,HttpServletResponse response){
		
		try {
			
			response.setHeader("Access-Control-Allow-Origin", "*");
			response.setContentType("application/json;charset=utf-8");
			JSONObject returnJson = new JSONObject();
			returnJson.put("falg","true");
			
			try {
				Integer deptid = Integer.valueOf(request.getParameter("deptid"));
				if(deptid != null){
					returnJson.put("deptid", deptid);
				}
			} catch (Exception e) {
				
			}
			driversService.mobSelectAllByCount(request);
			returnJson.put("driverList", request.getAttribute("driverList"));
			returnJson.put("page", request.getAttribute("mobilePage"));
			
			response.getWriter().print(returnJson.toString());
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
	/**
	 * 根据id删除驾驶员信息
	 * @param request
	 * @return
	 */
	@RequestMapping("/deleteDrivers")
	public String deleteDrivers(HttpServletRequest request){
		
		try {
			
			
		} catch (Exception e) {
			
		}
		
		Integer dId = Integer.valueOf(request.getParameter("did"));
		driversService.deleteDrivers(dId);
		return "redirect:/drivers/add_driver?pageNow=1";
	}
	

}
